<%@ page language="java" pageEncoding="UTF-8"%>
<%@ include file="/page/base.jsp"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@taglib prefix="d" uri="/doone-comm-ui-tags"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"></base>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title></title>
<!-- Libraries -->
<link type="text/css" href="<%=path%>/manage/css/layout.css"
	rel="stylesheet" />
<link type="text/css" href="<%=path%>/manage/css/new_style.css"
	rel="stylesheet" />
<link rel="stylesheet" type="text/css"
	href="<%=path%>/manage/css/Tooltip.css" />
<link type="text/css"
	href="<%=path%>/manage/page/user/css/users_style.css" rel="stylesheet" />
<script type="text/javascript"
	src="<%=path%>/manage/js/jquery-1.4.4.min.js"></script>
<script type="text/javascript" src="<%=path%>/manage/js/jquery_form.js"></script>
<script type="text/javascript" src="<%=path%>/manage/js/easyTooltip.js"></script>
<script type="text/javascript"
	src="<%=path%>/manage/js/jquery.wysiwyg.js"></script>
<script type="text/javascript" src="<%=path%>/manage/js/hoverIntent.js"></script>
<script type="text/javascript" src="<%=path%>/manage/js/superfish.js"></script>
<script type="text/javascript" src="<%=path%>/manage/js/custom.js"></script>
<script type="text/javascript"
	src="<%=path%>/manage/js/jquery-ui-1.8.11.custom.min.js"></script>
<script type="text/javascript" src="<%=path%>/manage/js/Tooltip.js"></script>
<script type="text/javascript" src="<%=path%>/manage/js/errorTip.js"></script>
<script type="text/javascript"
	src="<%=path%>/manage/js/jquery.validate.min.js"></script>
<script type="text/javascript"
	src="<%=path%>/manage/js/jquery.validate.methods.min.js"></script>
<script type="text/javascript"
	src="<%=path%>/manage/js/jquery.validate.defaults.js"></script>
<style type="text/css">
.target {
	display: none;
}
</style>
<script type="text/javascript">
	$(function() {
		var selChange = function() {
			var $this = $(this);
			$("td.target").hide();
			$("select[name='pointConfig.targetId']").rules("remove");
			$("select[name='pointConfig.targetId']").get(0).selectedIndex = 0;
			if ($this.val() == "FIELD_FILL_IN") {
				$("input[name='pointConfig.maxIn']").val(1);
				$("td.maxIn").hide();
			} else {
				$("input[name='pointConfig.maxIn']").val("");
				$("td.maxIn").show();
			}
			if ($this.val() != "USER_LOGIN" && $this.val() !== "USER_REGISTER"
					&& $this.val() != "FIELD_FILL_IN") {
				$("td.target").show();
				if ($this.val() == "ACTIVITIES_SHARE") {
					return;
				}
				$("select[name='pointConfig.targetId']").rules("add", {
					required : true,
					messages : {
						required : "请选择操作对象"
					}
				});
			}
		};
		$.validator.addMethod("maxIn", function(value, element) {
			return this.optional(element) || /^[0-9]+$/.test(value)
					|| /^[0-9]+\/m|h|d|M|y$/.test(value);
		}, "输入格式为n/* ,*可以是m:分钟,h:小时,d:天,M:月,y:月 n 为限制数字");
		var addPointDialogOpts = {
			autoOpen : false,
			width : 550,
			height : 300,
			modal : true,
			title : "添加积分规则",
			resizable : false,
			buttons : {
				"保存" : function() {
					var edit = $("#editFlag").val();
					if (edit == "edit") {
						$("form#addPointConfig").attr("action",
								root + "/clientuser/updateUserLevel.action");
						$("form#addPointConfig").submit();
					} else {
						var $form = $("form#addPointConfig");
						subAdd($form);
					}
				}
			},
			close : function(event, ui) {
				$("form#addPointConfig").clearForm();
				$("td.target").hide();
			}
		};
		var validOpts = {
			rules : {
				"pointConfig.actionType" : {
					required : true
				},
				"pointConfig.addPoints" : {
					required : true,
					number : true
				},
				"pointConfig.maxIn" : {
					required : true,
					maxIn : true
				}
			},
			messages : {
				"pointConfig.actionType" : {
					required : "请选择类型"
				},
				"pointConfig.addPoints" : {
					required : "增加积分必填",
					number : "必需是数字"
				},
				"pointConfig.maxIn" : {
					required : "积分增加限制必填",
					number : "必需是数字"
				}
			}
		};
		$("form#addPointConfig").validate(validOpts);
		$("div#addPointDialog").dialog(addPointDialogOpts);
		$("select[name='pointConfig.actionType']").change(selChange);
	});
	function subAdd($form) {
		if (!$form.valid()) {
			return;
		}
		var url = $form.attr("action");
		$.ajax({
			type : "POST",
			url : url,
			data : $form.serialize(),
			success : function(data) {
				if (data == "success") {
					window.location = window.location;
				} else {
					alert("已存在该条规则请勿重复添加");
				}
			}
		});
	}
	function openAddPointConfig() {
		var $form = $("form#addPointConfig");
		var doEditUrl = "${pageContext.request.contextPath}/clientuser/addPointConfig.action";
		$form.attr("action", doEditUrl);
		$form.find("input[name='pointConfig.id']").remove();
		$("div#addPointDialog").dialog("open");
	}
	function deletePointConfig(id) {
		if (!confirm("确认删除?")) {
			return;
		}
		var url = "${pageContext.request.contextPath}/clientuser/deletePointConfig.action";
		$.post(url, {
			"pointConfig.id" : id
		}, function() {
			window.location = window.location;
		});
	}
	function editPointConfig(id) {
		var editCallback = function(data) {
			$("td.target").hide();
			var $actionType = $("select[name='pointConfig.actionType'] option[value='"
					+ data.actionType + "']");
			$actionType.attr("selected", true);
			if (data.actionType != "USER_LOGIN"
					&& data.actionType !== "USER_REGISTER"
					&& data.actionType != "FIELD_FILL_IN") {
				$("td.target").show();
				if (data.targetId != "null") {
					var $targetId = $("select[name='pointConfig.targetId'] option[value='"
							+ data.targetId + "']");
					$targetId.attr("selected", true);
					$("td.target").show();
				}
			}
			if (data.actionType == "FIELD_FILL_IN") {
				$("input[name='pointConfig.maxIn']").val(1);
				$("td.maxIn").hide();
			} else {
				$("input[name='pointConfig.maxIn']").val(data.maxIn);
				$("td.maxIn").show();
			}
			$("input[name='pointConfig.addPoints']").val(data.addPoints);
			$("div#addPointDialog").dialog("open");
			$("form#addPointConfig").find("input[name='pointConfig.id']")
					.remove();
			var $id = $("<input type='hidden' name='pointConfig.id' value='"+data.id+"' />");
			$("form#addPointConfig").append($id);
			var doEditUrl = "${pageContext.request.contextPath}/clientuser/doEditPointConfig.action";
			$("form#addPointConfig").attr("action", doEditUrl);
		};
		var url = "${pageContext.request.contextPath}/clientuser/editPointConfig.action";
		$.get(url, {
			"pointConfig.id" : id
		}, editCallback);
	}
</script>
</head>
<body>
	<div>
		<p class="sitemap">当前位置：用户信息管理 > 积分规则配置</p>
		<h1>积分规则配置</h1>
		<div>
			<!-- Tabs -->
			<div id="tabs">
				<ul>
					<li><a href="#tabs-1">积分规则配置</a></li>
				</ul>
				<div class="bottom_line">
					<a href="javascript:openAddPointConfig();" class="btn_addnew">新增积分规则</a>
				</div>
				<table class="fullwidth" cellpadding="0" cellspacing="0">
					<thead>
						<tr>
							<td>类型</td>
							<td>操作对象</td>
							<td>积分</td>
							<td>限制</td>
							<td>创建日期</td>
							<td>操作</td>
						</tr>
					</thead>
					<tbody>
						<c:forEach var="config" items="${pointConfigs }">
							<tr class="odd">
								<td>${actionTypeNames[config.actionType] }</td>
								<td>${actionTypeTargetNames[config.targetId]}</td>
								<td>${config.addPoints }</td>
								<td><c:choose>
										<c:when test="${fn:indexOf(config.maxIn,'/')!=-1 }">
										      ${fn:substringBefore(config.maxIn,"/") }次/${timeKeyNames[fn:substringAfter(config.maxIn,"/")]}
										</c:when>
										<c:when test="${config.maxIn eq null}">
										        无限制
										</c:when>
										<c:otherwise>
                                               ${config.maxIn}次
										</c:otherwise>
									</c:choose></td>
								<td><fmt:formatDate value="${config.createDt}"
										pattern="yyyy-MM-dd hh:mm:ss" /></td>

								<td><a class="icon icon_edit" title="编辑"
									href="javascript:editPointConfig(${config.id })"></a> <a
									class="icon icon_delete" title="删除"
									href="javascript:deletePointConfig(${config.id })"></a></td>
							</tr>
						</c:forEach>
					</tbody>
				</table>
				<!-- 分页标签开始 -->
				<div align="right" style="float: right;">
					<d:page id="pagePoint" template="details" skin="gray">
					</d:page>
				</div>
				<!-- 分页标签结束 -->
			</div>
		</div>
	</div>
</body>
<!-- 新增等级dialog -->
<div id="addPointDialog" style="display: none;">
	<form id="addPointConfig">
		<table border="0" cellspacing="1" cellpadding="0"
			style="line-height: 50px">
			<tr>
				<td>类型 <select name="pointConfig.actionType">
						<option value="">-请选择类型-</option>
						<c:forEach var="actionTypeName" items="${actionTypeNames }">
							<option value="${actionTypeName.key }">
								${actionTypeName.value }</option>
						</c:forEach>
				</select>
				</td>
				<td class="target">操作对象 <select name="pointConfig.targetId">
						<option value="">-请选择操作对象-</option>
						<c:forEach var="actionTypeTargetName"
							items="${actionTypeTargetNames }">
							<option value="${actionTypeTargetName.key }">
								${actionTypeTargetName.value }</option>
						</c:forEach>
				</select>
				</td>
			</tr>
			<tr>
				<td>积分 <input name="pointConfig.addPoints" size="20"
					maxlength="5">
				</td>
			</tr>
			<tr>
				<td class="maxIn">限制 <input name="pointConfig.maxIn" size="20"
					maxlength="8">
				</td>
			</tr>
		</table>
	</form>
</div>
</html>
